/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.BugRS.dao;

import com.aptech.BugRS.enity.OrderDTO;
import com.aptech.BugRS.util.Configure;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author KISS
 */
public class OrderDAO {

    private Configure db = null;
    private Connection con = null;
    private ResultSet rs = null;
    private PreparedStatement pst = null;
    private String lastError = "";

    /**
     * Creates a new instance of D_Order
     */
    public OrderDAO() {
        db = new Configure();
    }

    /**
     *
     * @return list of Order
     */
    public ArrayList<OrderDTO> ReadByAll() {

        ArrayList<OrderDTO> list = new ArrayList<OrderDTO>();
        con = db.getConnection();
        String sql = "SELECT * FROM Complaint";
        try {
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                OrderDTO order = new OrderDTO();
                order.setOrderID(rs.getInt(1));
                order.setCustomerID(rs.getInt(2));
                order.setOrderDate(rs.getDate(3));
                order.setOrderStatus(rs.getString(4));
                list.add(order);
            }
        } catch (SQLException ex) {
            setLastError("SQL Error");
        } finally {
            db.closeConnection();
        }
        return list;
    }

    /**
     * Read by ID of Order
     * @param id
     * @return Order
     */
    public OrderDTO ReadByID(int id) {
        OrderDTO order = new OrderDTO();
        try {
            con = db.getConnection();
            String sql = "SELECT * FROM Order WHERE OrderID = ?";
            pst = con.prepareStatement(sql);
            pst.setInt(1, id);
            rs = pst.executeQuery();
            order.setOrderID(rs.getInt(1));
            order.setCustomerID(rs.getInt(2));
            order.setOrderDate(rs.getDate(3));
            order.setOrderStatus(rs.getString(4));

        } catch (SQLException ex) {
            setLastError("SQL Error");
        }
        return order;
    }

    /**
     * Method add new complaint
     * @param com
     * @return boolean
     */
    public boolean Create(OrderDTO order) {

        ArrayList<OrderDTO> list = ReadByAll();
        /*
         * Check exits in database
         */
        for (OrderDTO or : list) {
            if (or.getOrderID() == or.getOrderID()) {
                setLastError("Complaint is exitsed! Add fail!");
                return false;
            }
        }
        try {
            con = db.getConnection();
            String sql = "INSERT INTO Complaint VALUES(?,?,?,?,?,?,?)";
            pst = con.prepareStatement(sql);
            pst.setInt(1, order.getOrderID());
            pst.setInt(2, order.getCustomerID());
            pst.setDate(3, order.getOrderDate());
            pst.setString(4, order.getOrderStatus());
            if (pst.executeUpdate() > 0) {
                setLastError("Add Complaint Successfully!");
                db.closeConnection();
                return true;
            }
        } catch (SQLException ex) {
            setLastError("SQL Error!");
            db.closeConnection();
            return false;
        }
        setLastError("Add fail!");
        db.closeConnection();
        return false;
    }

    /**
     * Method Update a Complaint
     * @param com
     * @return boolean
     */
    public boolean Update(OrderDTO order) {
        try {
            con = db.getConnection();
            String sql = "SELECT * FROM Order WHERE OrderID = ?";
            pst = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            pst.setInt(1, order.getOrderID());
            rs = pst.executeQuery();
            rs.first();
            rs.updateInt(2, order.getCustomerID());
            rs.updateDate(3, order.getOrderDate());
            rs.updateString(4, order.getOrderStatus());
            rs.updateRow();
            db.closeConnection();
            setLastError("Update Comlaint successfully!");
            return true;
        } catch (SQLException ex) {
            setLastError("SQL Error");
            db.closeConnection();
            return false;
        }

    }

    public boolean isExits(OrderDTO order){
        try {
            String sql = "SELECT * FROM Oder WHERE OrderID = ?";
            con = db.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, order.getOrderID());
            rs = pst.executeQuery();
            if(rs.next()){
                return true;
            }
        } catch (SQLException ex) {
            setLastError("SQL Error");
        }
        return false;

    }
    public boolean Delete(int id) {
        return true;
    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }
}
